package zj.xinxin.business.business.articale;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import zj.xinxin.api.moudle.article.api.ArticleTagReferencedService;
import zj.xinxin.api.moudle.article.api.TagService;
import zj.xinxin.api.moudle.article.dto.TagDto;
import zj.xinxin.api.moudle.article.entity.ArticleTagReferencedEntity;
import zj.xinxin.api.moudle.article.entity.TagEntity;
import zj.xinxin.api.moudle.article.vo.TagPageSearchVo;
import zj.xinxin.api.moudle.article.vo.TagVo;
import zj.xinxin.business.business.articale.converter.TagConverter;
import zj.xinxin.common.result.PageResult;
import zj.xinxin.common.utils.BeanCopyUtil;
import zj.xinxin.mapper.mapper.article.TagMapper;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 标签  服务实现类
 * </p>
 *
 * @author zhoujun
 * @since 2022-04-12
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, TagEntity> implements TagService {

    @Autowired
    ArticleTagReferencedService tagReferencedService;

    @Override
    public Boolean addByVo(TagVo tag) {

        TagEntity tagEntity = TagConverter.INSTANCE.toEntity(tag);
        return this.save(tagEntity);
    }

    @Override
    public List<TagDto> getByArticleId(Long articleId) {
        if (Objects.isNull(articleId) || articleId <= 0L){
            return Collections.emptyList();
        }

        List<ArticleTagReferencedEntity> referencedList = tagReferencedService.list(
                new LambdaQueryWrapper<ArticleTagReferencedEntity>()
                .eq(ArticleTagReferencedEntity::getArticleId, articleId));
        if (CollectionUtils.isEmpty(referencedList)) {
            return Collections.emptyList();
        }
        List<Long> tagIdList = referencedList.stream()
                .map(ArticleTagReferencedEntity::getTagId)
                .collect(Collectors.toList());
        List<TagEntity> resultEntities = this.list(new LambdaQueryWrapper<TagEntity>()
                .in(TagEntity::getTagId, tagIdList));
        if (CollectionUtils.isEmpty(resultEntities)) {
            return Collections.emptyList();
        }
        return resultEntities.stream()
                .map(TagConverter.INSTANCE::toDto)
                .collect(Collectors.toList());
    }

    @Override
    public PageResult<TagDto> page(TagPageSearchVo search) {
        Map<String, Object> parameter = BeanCopyUtil.objectToMap(search);
        IPage<TagEntity> tagIPage = this.baseMapper.searchPageByCondition(
                new Page<>(search.getCurrent(), search.getSize()), parameter);
        IPage<TagDto> result = tagIPage.convert(TagConverter.INSTANCE::toDto);
        return new PageResult<>(result.getRecords(), result.getTotal());
    }
}
